import actionTypes from './actionTypes'
import { getNewList } from '../server'


const startFectNewListServer = () => {
	return { type: actionTypes.START_PATCH_NEWSLIST_STORE }
}
const fetchNewListEnd = () => {
	return { type: actionTypes.PATCH_NEWSLIST_STORE_END }
}
const patchNewListSuc = (res) => {
	return {
		type: actionTypes.PATCH_NEWSLIST_STORE_SUCCESS,
		list: res
	}
}
const patchNewListFaill = (res) => {
	return {
		type: actionTypes.PATCH_NEWSLIST_STORE_SUCCESS,
		msg: res
	}
}
export const addReadCount = (key)=>{
	return {
		type:actionTypes.ADD_READCOUNT_NEWLIST_STORE,
		value:key
	}
}
export const setLoading = (flag)=>{
	return {
		type:actionTypes.SET_NEWSLIST_LOADING_STORE,
		value:flag
	}
}
export const getNewsList = () => {
	return (dispatch) => {
		dispatch(startFectNewListServer());
		getNewList().then(res => {
			if (res.status === 200) {
				dispatch(patchNewListSuc(res.data.list))
			}
		}).catch(err => {
			dispatch(patchNewListFaill(err))
		}).finally(res => {
			dispatch(fetchNewListEnd())
		})
	}
} 